#include <QPalette>
#include "musicListView.h"
#include "globalValue.h"


musicListView::musicListView(QWidget* parent) : QScrollArea(parent)
{
    this->setMinimumHeight(150);
    this->setMinimumWidth(100);
    this->setMaximumWidth(500);
    this->setVerticalScrollBar(&scrollBar);
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    QPalette palette;
    //palette.setColor(QPalette::Window, QColor(0, 0, 0, 0));
    palette.setColor(QPalette::Window, widgetBack);
    this->setPalette(palette);

    items = NULL;
    layout = NULL;

    palette.setColor(QPalette::Window, QColor(0, 0, 0, 0));

    widget.setPalette(palette);

    widget.setMinimumHeight(0);


}

int musicListView::setMusicAdapter(musicListAdapter *a)
{
    adapter = a;
    QObject::connect(adapter, SIGNAL(dataChange()), this, SLOT(adapterChanged()));
    this->adapterChanged();
    update();
}


void musicListView::adapterChanged()
{
    int dataNum = adapter->getLen();
    musicData* data = adapter->getData();
    musicListItem* tmpItems = items;

    if(layout != NULL)
    {
        delete layout;
        layout = NULL;
    }

    layout = new QVBoxLayout;
    layout->setSpacing(2);
    layout->setContentsMargins(3, 0, 11, 0);

    if(dataNum == 0)
    {
        //widget.setLayout(layout);
        this->setWidget(&widget);
        if(tmpItems != NULL)
        {
            delete [] tmpItems;
        }
        items = NULL;
        return;
    }



    items = new musicListItem[dataNum];

    for(int t = 0; t < dataNum; t ++)
    {
        items[t].setMusicData(data);
        data = data->next;

        QObject::connect(items+t, SIGNAL(deleteMusic(musicData*)),
                         this, SIGNAL(removeMusic(musicData*)));
        QObject::connect(items+t, SIGNAL(deleteMusic(musicData*)),
                         adapter, SLOT(remove(musicData*)));
        QObject::connect(items+t, SIGNAL(playMusic(musicData*)),
                         this, SIGNAL(sendPlayMusic(musicData*)));

        layout->addWidget(items+t);
    }
    widget.setLayout(layout);
    //widget.updateGeometry();
    //widget.re
    widget.setFixedHeight(dataNum*41);
    this->setWidget(&widget);

    if(tmpItems != NULL)
    {
        delete [] tmpItems;
    }
}


void musicListView::paintEvent(QPaintEvent *event)
{
    widget.setFixedWidth(this->width());
    QScrollArea::paintEvent(event);
}
